package com.ctshk.rpc.finance.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.finance.dto.advance.AdvancePaymentDetailDTO;
import com.ctshk.rpc.finance.dto.advance.AdvancePaymentManageApplyPageDTO;
import com.ctshk.rpc.finance.dto.advance.AdvancePaymentManageListDTO;
import com.ctshk.rpc.finance.dto.supplier.AccountSummaryDTO;
import com.ctshk.rpc.finance.entity.FinanceAdvancePayment;
import com.ctshk.rpc.finance.entity.FinanceAdvancePaymentApplySummary;
import com.ctshk.rpc.finance.mapper.FinanceAdvancePaymentApplyMapper;
import com.ctshk.rpc.finance.mapper.FinanceAdvancePaymentApplySummaryMapper;
import com.ctshk.rpc.finance.mapper.FinanceAdvancePaymentMapper;
import com.ctshk.rpc.finance.req.advance.AdvancePaymentIdReq;
import com.ctshk.rpc.finance.req.advance.AdvancePaymentListReq;
import com.ctshk.rpc.finance.req.supplier.AccountIdReq;
import com.ctshk.rpc.finance.service.IFinanceAdvancePaymentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 财务预收款管理 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-25
 */
@DubboService
public class FinanceAdvancePaymentServiceImpl extends ServiceImpl<FinanceAdvancePaymentMapper, FinanceAdvancePayment> implements IFinanceAdvancePaymentService {

    @Autowired
    private FinanceAdvancePaymentMapper advancePaymentMapper;
    @Autowired
    private FinanceAdvancePaymentApplySummaryMapper advancePaymentApplySummaryMapper;
    @Autowired
    private FinanceAdvancePaymentApplyMapper advancePaymentApplyMapper;


    /**
     * 列表
     *
     * @param req
     * @param currentUser
     * @return
     */
    @Override
    public Result<PageResponse<AdvancePaymentManageListDTO>> list(AdvancePaymentListReq req, TokenUser currentUser) {
        //TODO 权限校验
        Page<FinanceAdvancePayment> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<FinanceAdvancePayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(req.getBigClientId() != null,"supplier_id",req.getBigClientId());
        queryWrapper.eq("is_deleted",0);
    //    queryWrapper.eq("create_id",req.getUserId());
        queryWrapper.like(StringUtils.isNotBlank(req.getContactsName()),"supplier_contacts",req.getContactsName());
        queryWrapper.or();
        queryWrapper.like(StringUtils.isNotBlank(req.getContactsName()),"supplier_telephone",req.getContactsName());
        queryWrapper.orderByDesc("gmt_create");
        Page<FinanceAdvancePayment> advancePaymentPage = advancePaymentMapper.selectPage(page, queryWrapper);
        if (CollectionUtils.isEmpty(advancePaymentPage.getRecords())) {
            return Result.success(PageResponse.empty(advancePaymentPage.getCurrent()));
        }
        List<AdvancePaymentManageListDTO> list = new ArrayList<>();
        advancePaymentPage.getRecords().forEach(item -> {
            AdvancePaymentManageListDTO dto = EntityUtil.copy(item,AdvancePaymentManageListDTO.class);
            //预收款申请信息列表
            List<AdvancePaymentManageApplyPageDTO> applyList = advancePaymentApplySummaryMapper.queryByPayment(item.getId());
            dto.setApplyList(applyList);
            list.add(dto);
        });
        PageResponse<AdvancePaymentManageListDTO> result = new PageResponse<>(list,
                advancePaymentPage.getCurrent() == 1,
                advancePaymentPage.getTotal() <= (advancePaymentPage.getCurrent() * advancePaymentPage.getSize()),
                advancePaymentPage.getTotal(),
                advancePaymentPage.getCurrent(),
                advancePaymentPage.getSize());
        return Result.success(result);
    }


    /**
     * 查看-基本信息
     *
     * @param req
     * @return
     */
    @Override
    public Result<AdvancePaymentDetailDTO> detailBase(AdvancePaymentIdReq req) {
        //获取基本信息
        FinanceAdvancePayment payment = getById(req.getId());
        if (payment == null || Objects.equals(payment.getIsDeleted(), NumberConstant.ONE.getValue())){
            throw new BusinessException(SystemError.FINANCE_ADVANCE_17002);
        }
        AdvancePaymentDetailDTO dto = EntityUtil.copy(payment,AdvancePaymentDetailDTO.class);
        //匯總信息
        List<AdvancePaymentManageApplyPageDTO> applyList = advancePaymentApplySummaryMapper.queryByPayment(req.getId());
        dto.setApplyList(applyList);
        return Result.success(dto);
    }

    /**
     * 申请销账-查询客户预收款额度
     *
     * @param req
     * @return
     */
    @Override
    public Result<List<AccountSummaryDTO>> accountSummary(AccountIdReq req) {
        //查询剩余可用预收款列表
        List<AccountSummaryDTO> list = advancePaymentApplySummaryMapper.queryRemainAmountList(req.getAccountId());
        return Result.success(list);
    }


}
